package com.hy.mydesktop.client.component.factory;

import java.util.ArrayList;
import java.util.List;

import com.extjs.gxt.ui.client.data.BaseListLoadResult;
import com.extjs.gxt.ui.client.data.BaseListLoader;
import com.extjs.gxt.ui.client.data.ListLoader;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.data.RpcProxy;
import com.extjs.gxt.ui.client.dnd.DND.Feedback;
import com.extjs.gxt.ui.client.dnd.GridDragSource;
import com.extjs.gxt.ui.client.dnd.GridDropTarget;
import com.extjs.gxt.ui.client.store.ListStore;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.GridCellRenderer;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.hy.mydesktop.client.component.meta.ReorderingGridModelEnum;
import com.hy.mydesktop.shared.persistence.domain.gxt.GxtComponentMetaNodeModel;

/**
 * 
 * <ul>
 * <li>开发作者：冬超</li>
 * <li>设计日期：2010-8-30；时间：下午01:11:56</li>
 * <li>类型名称：ReorderingGridFactory</li>
 * <li>设计目的：</li>
 * </ul>
 * <ul>
 * <b>修订编号：</b>
 * <li>修订日期：</li>
 * <li>修订作者：</li>
 * <li>修订原因：</li>
 * <li>修订内容：</li>
 * </ul>
 */
public class ReorderingGridFactory {
	private static List<ColumnConfig> configs;
	private static GridCellRenderer<ModelData> ageRender;

	public static Grid<ModelData> ReorderingGrid(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {

		return configureReorderingGrid(gxtComponentMetaNodeModel);

	}

	/**
	 * 
	 * 
	 * @param gxtComponentMetaNodeModel
	 * @return
	 */
	private static Grid<ModelData> configureReorderingGrid(
			GxtComponentMetaNodeModel gxtComponentMetaNodeModel) {
		
		Grid<ModelData> grid = createDefaultReorderingGrid();
		GridDropTarget dropTarget = new GridDropTarget(grid);
		new GridDragSource(grid);

		if (gxtComponentMetaNodeModel.get(ReorderingGridModelEnum.HIGHT.name()
				.toLowerCase()) != null) {
			Integer height = (Integer) gxtComponentMetaNodeModel
					.get(ReorderingGridModelEnum.HIGHT.name().toLowerCase());
			grid.setHeight((int) height);

		} 
		if (gxtComponentMetaNodeModel.get(ReorderingGridModelEnum.WIDTH
				.name().toLowerCase()) != null) {
			Integer width = (Integer) gxtComponentMetaNodeModel
					.get(ReorderingGridModelEnum.WIDTH.name().toLowerCase());
			grid.setWidth((int) width);

		} 
		if (gxtComponentMetaNodeModel
				.get(ReorderingGridModelEnum.ENABLECOLUMNRESIZE.name()
						.toLowerCase()) != null) {
			Boolean enableColumnResize = (Boolean) gxtComponentMetaNodeModel
					.get(ReorderingGridModelEnum.ENABLECOLUMNRESIZE.name()
							.toLowerCase());
			grid.setEnableColumnResize(enableColumnResize);

		} 
		if (gxtComponentMetaNodeModel
				.get(ReorderingGridModelEnum.MINCOLUMNWIDTH.name()
						.toLowerCase()) != null) {
			Integer minColumnWidth = (Integer) gxtComponentMetaNodeModel
					.get(ReorderingGridModelEnum.MINCOLUMNWIDTH.name()
							.toLowerCase());
			grid.setMinColumnWidth(minColumnWidth);

		} 
		if (gxtComponentMetaNodeModel.get(ReorderingGridModelEnum.NUMBER
				.name().toLowerCase()) != null) {
			Integer number = (Integer) gxtComponentMetaNodeModel
					.get(ReorderingGridModelEnum.NUMBER.name().toLowerCase());
			getField(configs, ageRender, number);

		} 
		if (gxtComponentMetaNodeModel.get(ReorderingGridModelEnum.FEEBACK
				.name().toLowerCase()) != null) {
			String back = ((String) gxtComponentMetaNodeModel
					.get(ReorderingGridModelEnum.FEEBACK.name().toLowerCase()))
					.toUpperCase();
			if (back.equals("APPEND")) {
				dropTarget.setFeedback(Feedback.APPEND);
			} else if (back.equals("BOTH")) {
				dropTarget.setFeedback(Feedback.BOTH);
			}else{
				dropTarget.setFeedback(Feedback.INSERT);
			}

		}
		if (gxtComponentMetaNodeModel
				.get(ReorderingGridModelEnum.AllOWSELFASSOURCE.name()
						.toLowerCase()) != null) {
			Boolean allowSelfAsSource = (Boolean) gxtComponentMetaNodeModel
					.get(ReorderingGridModelEnum.AllOWSELFASSOURCE.name()
							.toLowerCase());
			dropTarget.setAllowSelfAsSource(allowSelfAsSource);
		} 


		return grid;
	}

	private static Grid<ModelData> createDefaultReorderingGrid() {

		RpcProxy<List<ModelData>> proxy = new RpcProxy<List<ModelData>>() {
			@Override
			protected void load(Object loadConfig,
					AsyncCallback<List<ModelData>> callback) {
//				DataServiceAsync service = GWT.create(DataService.class);
//				service.readData(callback);
			}
		};

		ListLoader<BaseListLoadResult<ModelData>> loader = new BaseListLoader<BaseListLoadResult<ModelData>>(
				proxy);
		ListStore<ModelData> employeeStore = new ListStore<ModelData>(loader);
		ageRender = new GridCellRenderer<ModelData>() {
			@Override
			public Object render(ModelData model, String property,
					com.extjs.gxt.ui.client.widget.grid.ColumnData config,
					int rowIndex, int colIndex, ListStore<ModelData> store,
					Grid<ModelData> grid) {
				return null;
			}
		};

		configs = new ArrayList<ColumnConfig>();

		ColumnModel cm = new ColumnModel(configs);

		Grid<ModelData> grid = new Grid<ModelData>(employeeStore, cm);

		return grid;
	}

	private static List<ColumnConfig> getField(List<ColumnConfig> configs,
			GridCellRenderer<ModelData> ageRender, int number) {
		for (int i = 0; i < number; i++) {
			ColumnConfig name = new ColumnConfig();
			name.setId("");
			name.setHeader("");
			name.setWidth(100);
			name.setRenderer(ageRender);
			configs.add(name);
		}
		return configs;
	}
}
